package com.smarttable.ysd;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.umeng.analytics.MobclickAgent;

public class GamePlayActivity extends AppCompatActivity implements OnClickListener{
    private Button btn_game,btn_video,btn_test;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_play);

        btn_game = (Button) findViewById(R.id.btn_game);
        btn_video = (Button) findViewById(R.id.btn_video);
        btn_test = (Button) findViewById(R.id.btn_test);

        btn_game.setOnClickListener(this);
        btn_video.setOnClickListener(this);
        btn_test.setOnClickListener(this);

        initXMLFile();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    //初始化配置文件
    private void initXMLFile(){
        //初始化游戏分类的配置文件
        SharedPreferences gameSP = getSharedPreferences(AppManager.CATEGORY_GAME_FILE, Context.MODE_PRIVATE);
        StringBuilder gameOldCns = new StringBuilder(gameSP.getString(AppManager.CATEGORY_GAME_FILE_KEY, "").trim());
        if (gameOldCns==null || gameOldCns.length()==0) {
            String gameCn = "com.pada.gamecenter/com.pada.gamecenter.activity.SplashActivity";
            gameOldCns.insert(0, gameCn);
            gameCn = AppManager.APP_DIVIDE+"com.netease.my/com.netease.my.AppActivity";
            gameOldCns.append(gameCn);
            String gameNewCns = gameOldCns.toString().trim();
            gameSP.edit().putString(AppManager.CATEGORY_GAME_FILE_KEY, gameNewCns).commit();
        }

        //初始化视频分类的配置文件
        SharedPreferences videoSP = getSharedPreferences(AppManager.CATEGORY_VIDEO_FILE, Context.MODE_PRIVATE);
        StringBuilder videoOldCns = new StringBuilder(videoSP.getString(AppManager.CATEGORY_VIDEO_FILE_KEY, "").trim());
        if (videoOldCns==null || videoOldCns.length()==0) {
            String videoCn = "com.mxtech.videoplayer.pro/com.mxtech.videoplayer.pro.ActivityMediaList";
            videoOldCns.insert(0, videoCn);
            videoCn = AppManager.APP_DIVIDE+"com.qiyi.video.pad/com.qiyi.video.pad.activity.WelcomeActivity";
            videoOldCns.append(videoCn);
            String videoNewCns = videoOldCns.toString().trim();
            videoSP.edit().putString(AppManager.CATEGORY_VIDEO_FILE_KEY, videoNewCns).commit();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_game:
                gotoCategoryPage(1);
                break;

            case R.id.btn_video:
                gotoCategoryPage(2);
                break;
            case R.id.btn_test:
                gotoCategoryPage(3);
                break;
            default:
                break;
        }

    }

    /*
  * category == 1 代表游戏btn
  * category == 2 代表视频btn
  */
    private void gotoCategoryPage(int category){
        Intent intent = new Intent();
        intent.setClass(GamePlayActivity.this, CategoryDetailActivity.class);
        Bundle extras = new Bundle();
        extras.putInt("CATEGORY", category);
        intent.putExtras(extras);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
